#include <fermat/common/buffer_slice.h>
#include <gtest/gtest.h>
#include <string>


TEST(net, buffer_slice_1)
{
    fermat::PageBufferSlice *pbuf = new fermat::PageBufferSlice();
    EXPECT_EQ(pbuf->writeAbleSize(), pbuf->capacity());
    delete pbuf; 
}

TEST(net, buffer_slice_2)
{
    fermat::PageBufferSlice *pbuf = new fermat::PageBufferSlice();
    EXPECT_EQ(pbuf->writeAbleSize(), pbuf->capacity());
    pbuf->append("hello world", sizeof("hello world") - 1);
    EXPECT_EQ(pbuf->size(), sizeof("hello world")-1);
    pbuf->retrieve(sizeof("hello world") - 2);
    EXPECT_EQ(pbuf->size(), 1);
    EXPECT_EQ(pbuf->writeAbleSize() + sizeof("hello world") - 1, pbuf->capacity());
    pbuf->retrieveAll();
    EXPECT_EQ(pbuf->writeAbleSize(), pbuf->capacity());
    delete pbuf; 
}

TEST(net, buffer_slice_3)
{
    fermat::PageBufferSlice *pbuf = new fermat::PageBufferSlice();
    EXPECT_EQ(pbuf->writeAbleSize(), pbuf->capacity());
    pbuf->append("hello world", sizeof("hello world") - 1);
    pbuf->retrieve(sizeof("hello ") - 1);
    EXPECT_EQ(pbuf->size(), sizeof("world") - 1);
    EXPECT_EQ(pbuf->writeAbleSize() + sizeof("hello world") - 1, pbuf->capacity());
    pbuf->shrink();
    EXPECT_EQ(pbuf->writeAbleSize(), pbuf->capacity() - sizeof("world") + 1);
    delete pbuf; 
}